<?php


namespace App\Http\Controllers;


use App\Http\Requests\Forward\FoodDetailRequest;
use App\Http\Requests\Forward\FoodListRequest;
use App\Http\Requests\Forward\FoodOrderDetailRequest;
use App\Models\PartitionTableRecord;
use App\Services\Java\ApiService;
use App\Services\Login;
use App\Services\Partition;
use D1M\Common\Exceptions\BusinessException;
use D1M\Common\Helpers\Result;
use Facade\FlareClient\Api;
use Illuminate\Http\Request;

class ForwardController
{

    /**
     * 转发java接口
     */

    /**
     * 菜单列表
     * @return array
     */
    public function foodList(FoodListRequest $request, ApiService $api)
    {
        $params = $request->only(['shopId']);
        $res = $api->requestJava('/food/api/food/list', $params, 'post');
        return Result::success($res);
    }

    /**
     * @param FoodDetailRequest $request
     * @param ApiService $api
     * @return array
     */
    public function foodDetail(FoodDetailRequest $request, ApiService $api)
    {
        $params = $request->only(['productOnlineId']);
        $res = $api->requestJava('/food/api/food/detail', $params, 'post');
        return Result::success($res);
    }

    /**
     *下单、结账
     */
    public function foodOrder(Request $request, ApiService $api)
    {
        $params = $request->only(['buyUser', 'shopId', 'foodNo', 'createUser', 'reduceId', 'reduceAmount']);
        $res = $api->requestJava('/order/api/order-record/create-food', $params, 'post');
        //失败回滚调整金额

        //成功则添加到开台记录中
        //PartitionTableRecord::where('food_order_no',$params['foodNo'])->update(['order_no'=>$res['no']]);
        return Result::success($res);
    }

    /**
     * 订单详情
     */
    public function foodOrderDetail(FoodOrderDetailRequest $request, ApiService $api)
    {
        $params = $request->only(['no']);
        $res = $api->requestJava('/order/api/order-record/info', $params, 'post');
        return Result::success($res);
    }


    /**
     * 新增餐饮单
     */
    public function addOrder(Request $request, ApiService $api)
    {
        $params = $request->only(['storeId', 'deskId', 'detailList']);
        $res = $api->requestJava('/food/api/order/add', $params, 'post');
        return Result::success($res);
    }

    /**
     * 新增餐饮单,餐饮单确认
     */
    public function foodOrderAdd(Request $request, ApiService $api)
    {
        $params = $request->only(['storeId', 'deskRecordId', 'orderNo', 'detailList', 'deskNo', 'manageNo']);
        $res = $api->requestJava('/food/api/order/create', $params, 'post');
        return Result::success($res);
    }

    /**
     * 餐饮单加菜
     */
    public function addSku(Request $request, ApiService $api)
    {
        $params = $request->only(['orderNo', 'detailList']);
        $res = $api->requestJava('/food/api/order/add-sku', $params, 'post');
        return Result::success($res);
    }

    /**
     * 餐饮单退菜
     */
    public function delSku(Request $request, ApiService $api)
    {
        $params = $request->only(['orderNo', 'detailList']);
        $res = $api->requestJava('/food/api/order/del-sku', $params, 'post');
        return Result::success($res);
    }

    /**
     * 餐饮单详情
     */
    public function orderDetail(Request $request, ApiService $api)
    {
        $params = $request->only(['orderNo']);
        $res = $api->requestJava('/food/api/order/detail', $params, 'post');
        return Result::success($res);
    }

    /**
     * 根据桌号ID获取餐饮单号 废弃
     */
    public function getOrderNo__(Request $request, ApiService $api)
    {
        $params = $request->only(['storeId', 'deskId']);
        $res = $api->requestJava('/food/api/order/getByDesk', $params, 'post');
        return Result::success($res);
    }

    /**
     * 根据桌号ID列表获取餐饮单
     */
    public function getOrderInfo(Request $request, ApiService $api)
    {
        $params = $request->only(['deskRecordIds']);
        $res = $api->requestJava('/food/api/order/founding-info', $params, 'post');
        return Result::success($res);
    }

    /**
     * 根据开台ID获取餐饮单信息
     */
    public function getOrderList(Request $request, ApiService $api)
    {
        $params = $request->only(['deskRecordId']);
        $res = $api->requestJava('/food/api/order/getByDesk', $params, 'post');
        return Result::success($res);
    }


    /**
     * 打印餐饮单列表
     * @param Request $request
     * @param ApiService $api
     * @return array
     */
    public function printOrderList(Request $request, ApiService $api)
    {
        $params = $request->only(['storeId', 'printType']);
        $res = $api->requestJava('/food/open/order/print-info/list', $params, 'post');
        return Result::success($res);
    }

    /**
     * 打印餐饮单标签
     * @param Request $request
     * @param ApiService $api
     * @return array
     */
    public function printOrderTag(Request $request, ApiService $api)
    {
        $params = $request->only(['orderNo']);
        $res = $api->requestJava('/food/open/order/print/tag', $params, 'post');
        return Result::success($res);
    }

    /**
     * 打印餐饮单小票
     * @param Request $request
     * @param ApiService $api
     * @return array
     */
    public function printOrderReceipt(Request $request, ApiService $api)
    {
        $params = $request->only(['orderNo', 'printOrderType']);
        $res = $api->requestJava('/food/api/print/receipt', $params, 'post');
        return Result::success($res);
    }

    /**
     * 打印部分标签
     * @param Request $request
     * @param ApiService $api
     * @return array
     */
    public function printPartTag(Request $request, ApiService $api)
    {
        $params = $request->only(['skuDetails', 'orderNo']);
        $res = $api->requestJava('/food/api/print/tag', $params, 'post');
        return Result::success($res);
    }


    /**
     * 用户查询列表 /auth/api/user/userInfoByPhone
     * 支持模糊查询
     * @param Request $request
     * @param ApiService $api
     * @return array
     */
    public function getUserList(Request $request, ApiService $api)
    {
        $params = $request->only(['phone', 'pageSize', 'pageNo', 'shopId']);
        $res = $api->requestJava('/auth/api/user/userInfoByPhone', $params, 'post');
        return Result::success($res);
    }

    /**
     * 用户列表 不排序
     * 支持模糊查询
     * @param Request $request
     * @param ApiService $api
     * @return array
     */
    public function userList(Request $request, ApiService $api)
    {
        $params = $request->only(['phone', 'pageSize', 'pageNo']);
        $res = $api->requestJava('/auth/api/user/query-user-info', $params, 'post');
        return Result::success($res);
    }

    /**
     * 根据唯一标识获取用户信息 /auth/api/user/userInfoByManageNo
     * @param Request $request
     * @param ApiService $api
     * @return array
     */
    public function getUserByManageNo(Request $request, ApiService $api)
    {
        $params = $request->only(['manageNo']);
        $res = $api->requestJava('/auth/api/user/userInfoByManageNo', $params, 'post');
        return Result::success($res);
    }

    /**
     * 用户台面信息
     * @param Request $request
     * @param ApiService $api
     * @return array
     */
    public function getUserOrderDetail(Request $request, ApiService $api, Partition $service, Login $login)
    {
        $params = $request->only(['manageNo']);
        //用户详情
        $user = $api->requestJava('/auth/api/user/userInfoByManageNo', $params, 'post');

        //用户台面信息
        $tableInfos = $service->getTableRecordByManageNo($params['manageNo']);
        $tableIds = array_column($tableInfos, 'partition_table_record_id');
//        $me = $login->me($request);
//        $storeId =  $me->shop_id;

        //餐饮单对应的金额
        if ($tableIds) {
            $foodOrderInfos = $api->requestJava('/food/api/order/founding-info', [
                'deskRecordIds' => $tableIds,
                //'storeId'=>$storeId
            ], 'post');

            $amount = array_column($foodOrderInfos, 'sumPrice', 'deskRecordId');
            foreach ($tableInfos as $v) {
                if ($v->partition_table_record_id && isset($amount[$v->partition_table_record_id])) {
                    $v->sum_price = $amount[$v->partition_table_record_id];
                }
            }
        }

        $res = [
            'user' => $user,
            'foodOrder' => $tableInfos,
        ];
        return Result::success($res);
    }

    /**
     * 用户消费记录
     */
    public function userConsumptionRecords(Request $request, ApiService $api)
    {
        $params = $request->only(['manageNo', 'pageNo', 'pageSize']);
        $res = $api->requestJava('/order/api/order-record/consumption-records', $params, 'post');
        return Result::success($res);
    }


    /**
     * 扫码获取零售商品
     */
    public function getProductInfo(Request $request, ApiService $api)
    {
        $params = $request->only(['id']);
        $res = $api->requestJava('/product/api/product/info', $params, 'post');
        return Result::success($res);
    }

    /**
     * 根据SKU上架的id集合 获取零售商品
     */
    public function getProductInfoByRetailOnlineId(Request $request, ApiService $api)
    {
        $params = $request->only(['specRetailOnlineId']);
        $res = $api->requestJava('/product/api/product/queryBySkuIds', $params, 'post');
        return Result::success($res);
    }

    /**
     * 提交零售订单
     */
    public function createRetail(Request $request, ApiService $api)
    {
        $params = $request->only(['manageNo', 'shopId', 'specOnlineAddDTOList', 'productRetailTemporaryAddDTO', 'createUser', 'type', 'orderNo']);
        $res = $api->requestJava('/product/api/product/create-retail', $params, 'post');
        return Result::success($res);
    }

    /**
     * 编辑零售订单
     */
    public function editRetail(Request $request, ApiService $api)
    {
        $params = $request->only(['orderNo', 'reduceId', 'reduceAmount', 'adjustAmount', 'reduceName']);
        $res = $api->requestJava('/product/api/product/update', $params, 'post');
        return Result::success($res);
    }

    /**
     * 挂起零售订单列表
     */
    public function getPendingRetailList(Request $request, ApiService $api)
    {
        $params = $request->only(['shopId', 'posDeviceId']);
        $res = $api->requestJava('/product/api/product/retail-pending-list', $params, 'post');
        return Result::success($res);
    }

    /**
     * 挂起零售订单列表（带分页）
     */
    public function getPendingRetailPage(Request $request, ApiService $api)
    {
        $params = $request->only(['shopId', 'posDeviceId', 'pageNo', 'pageSize']);
        $res = $api->requestJava('/product/api/product/retail-pending-page', $params, 'post');
        return Result::success($res);
    }

    /**
     * 清空零售订单
     */
    public function delRetail(Request $request, ApiService $api)
    {
        $params = $request->only(['orderNo']);
        $res = $api->requestJava('/product/api/product/resetOrder', $params, 'post');
        return Result::success($res);
    }

    /**
     * 零售订详情（提交订单页面，零售单详情）
     */
    public function getRetailDetail(Request $request, ApiService $api)
    {
        $params = $request->only(['orderNo']);
        $res = $api->requestJava('/product/api/product/retail-order-detail', $params, 'post');
        return Result::success($res);
    }

    /**
     * 查询储值卡列表
     */
    public function memberCard(ApiService $api)
    {
        //$params =$request->only(['barCode']);
        $res = $api->requestJava('/card/api/order/member/cardList', [], 'post');
        return Result::success($res);
    }

    /**
     * 充值
     */
    public function orderCard(Request $request, ApiService $api)
    {
        $params = $request->only(['cardId', 'rechargeId', 'manageNo']);
        $res = $api->requestJava('/card/api/order/buy', $params, 'post');
        return Result::success($res);
    }

    /**
     * 零售商品结账
     */
    public function createProductOrder(Request $request, ApiService $api)
    {
        $params = $request->only(['buyUser', 'shopId', 'retailNo', 'createUser']);
        $res = $api->requestJava('/order/api/order-record/create-retail', $params, 'post');
        return Result::success($res);
    }


    /**
     * 用户优惠券列表
     */
    public function userVoucherList(Request $request, ApiService $api)
    {
        $params = $request->only(['user', 'shopId', 'businessNo', 'businessType']);
        $res = $api->requestJava('/content/api/user-voucher/use-list', $params, 'post');
        return Result::success($res);
    }


    /**
     * 用户优惠金额计算
     */
    public function userVoucherCalculate(Request $request, ApiService $api)
    {
        $params = $request->only(['user', 'shopId', 'businessNo', 'businessType', 'voucherId']);
        $res = $api->requestJava('/content/api/user-voucher/money-calculate', $params, 'post');

        return Result::success($res);
    }

    /**
     * 订单列表
     */
    public function orderList(Request $request, ApiService $api)
    {
        $params = $request->only(['state', 'todayFlag', 'pageNo', 'pageSize', 'storeId','no']);
        $res = $api->requestJava('/order/api/order-record/order-paging', $params, 'post');
        return Result::success($res);
    }

    /**
     * 订单列表#恢复下单
     */
    public function orderRollback(Request $request, ApiService $api)
    {
        $params = $request->only(['no']);
        $res = $api->requestJava('/order/api/order-record/order-rollback', $params, 'post');
        return Result::success($res);
    }

    public function foodStockList(Request $request, ApiService $api)
    {
        $params = $request->only(['productSpecOnlineIds']);
        $res = $api->requestJava('/food/api/food/stockList', $params, 'post');
        return Result::success($res);
    }

    //搜索零售sku列表
    public function productSkuList(Request $request, ApiService $api)
    {
        $params = $request->only(['name', 'shopId', 'pageNo', 'pageSize']);
        $res = $api->requestJava('/product/api/product/sku-list', $params, 'post');
        return Result::success($res);
    }

    public function foodOrderRefund(Request $request, ApiService $api)
    {
        $params = $request->only(['no','createUser']);
        $res = $api->requestJava('/order/api/order-record/refund', $params, 'post');
        return Result::success($res);
    }

    public function foodOrderClose(Request $request, ApiService $api)
    {
        $params = $request->only(['no']);
        $res = $api->requestJava('/order/api/order-record/close', $params, 'post');
        return Result::success($res);
    }

    public function foodOrderStatistics(Request $request, ApiService $api)
    {
        $params = $request->only(['storeId']);
        $res = $api->requestJava('/order/api/order-record/statistics', $params, 'post');
        return Result::success($res);
    }

}